import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as cookieParser from 'cookie-parser'
import * as session from 'express-session'
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';
import { engine } from 'express-handlebars';
import { ValidationPipe } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule,{
    logger:['error','warn']
  });
  // 静态资源
  app.useStaticAssets(join(__dirname, '..', 'public'))
  // 模板
  app.setBaseViewsDir(join(__dirname, '..', 'views'))
  app.engine('hbs',engine({
    extname: '.hbs',
    runtimeOptions: {
      allowProtoPropertiesByDefault: true, // 允许访问__proto__ 属性
      allowProtoMethodsByDefault: true,// 允许访问__proto__ 方法
    },
  }))
  app.setViewEngine('hbs')
  app.use(cookieParser())
  app.use(session({
    secret: 'keyboard',
    resave: true,
    saveUninitialized: true,
    cookie: {
      maxAge: 3600000 *24 * 7,
    }
  }))
  app.useGlobalPipes(new ValidationPipe({
    transform:true //打开后 参数会由普通对象变成类的实例
  }))
  const config  = new DocumentBuilder()
    .setTitle('CMS API')
    .setDescription('CMS API 描述')
    .setVersion('1.0')
    .addTag('CMS')
    .addCookieAuth('connect.sid')
    .addBearerAuth({
      type: 'http',
      scheme: 'bearer',
      bearerFormat: 'JWT',
      name: 'JWT',
      description: '输入token',
      in: 'header',
    })
    .build()
  const document = SwaggerModule.createDocument(app,config)
  SwaggerModule.setup('api-doc',app,document)
  await app.listen(3000);
}
bootstrap();
